package plus.lgx.proj1

import java.util
import scala.collection.immutable.HashMap

object PlayMode {
  private val mapper = (HashMap.newBuilder[String, Int]
    += (("顺序播放", 0)) += (("随机播放", 1))).result()
}
class PlayMode(val name: String) {
  val id: Int = PlayMode.mapper(name)
}

class Music(val musicName: String, val length: Int, val author: String, val album: String)

class MusicPlayer(val musicList: util.ArrayList[Music], var musicIndex: Int, var playMode: PlayMode) {

  import java.util.Scanner

  def play(): Unit = {
    if (musicList.isEmpty) {
      println("当前音乐列表中暂无音乐，无法播放。")
      return
    }
    println("开始播放音乐 ...")
    val currentMusic = musicList.get(musicIndex)
    println(s"当前播放的是 ${currentMusic.musicName}, 时长 ${parseMusicLength(currentMusic.length)}")
  }

  private def createMusic(): Music = {
    val s = new Scanner(System.in)
    print("请输入要添加的音乐的名称：")
    val musicName = s.nextLine
    print("请输入音乐时长：")
    val musicLength = s.nextLine
    print("请输入音乐作者：")
    val musicAuthor = s.nextLine
    print("请输入专辑：")
    val musicAlbum = s.nextLine

    new Music(musicName, parseMusicLength(musicLength), musicAuthor, musicAlbum)
  }

  def addMusic(): Boolean = {
    try {
      musicList.add(createMusic())
      printMusicList()
      println(s"添加音乐成功，当前音乐列表中共有 ${musicList.size} 首音乐。")
      true
    } catch {
      case _: Throwable =>
        println("添加音乐失败。")
        false
    }
  }

  def removeMusic(): Boolean = {
    try {
      println("正在删除音乐。")
      printMusicList()
      val s = new Scanner(System.in)
      print("请输入要删除的音乐的序号：")
      val ind = s.nextInt
      if (ind <= 0 || ind > musicList.size) {
        println("已取消。")
        return false
      }
      musicList.remove(ind - 1)
      println(s"删除音乐成功，当前音乐列表中共有 ${musicList.size} 首音乐。")
      printMusicList()
      true
    } catch {
      case _: Throwable =>
        println("删除音乐失败。")
        false
    }
  }

  def stop(): Unit = {
    println("暂停播放。")
  }

  def printMusicList(): Unit = {
    var ind = 0
    println("序号\t名称\t时长\t作者\t专辑")
    import scala.collection.JavaConversions._
    musicList.foreach((i: Music) =>  {
      ind += 1
      println(s"$ind\t${i.musicName}\t${i.length}\t${i.author}\t${i.album}")
    })
  }

  private def parseMusicLength(musicLength: Int): String = {
    val hours = musicLength / 60 / 60
    val minutes = musicLength / 60 % 60
    val seconds = musicLength % 60
    if (hours == 0) {
      if (minutes == 0)
        seconds + ""
      else
        minutes + ":" + seconds
    } else
      hours + ":" + minutes + ":" + seconds
  }

  private def parseMusicLength(musicLength: String): Int = {
    val split = musicLength.split(":").map((s: String) => s.toInt)

    if (split.length == 1)
      split(0)
    else if (split.length == 2)
      split(0) * 60 + split(1)
    else
      (split(0) * 60 + split(1)) * 60 + split(2)
  }
}

object MusicPlayer {

  def main(args: Array[String]): Unit = {
    val musicPlayer = new MusicPlayer(new util.ArrayList[Music], 0, new PlayMode("顺序播放"))
    musicPlayer.play()
    musicPlayer.addMusic()
    musicPlayer.addMusic()
    musicPlayer.play()
    musicPlayer.removeMusic()
    musicPlayer.stop()
  }

}
